<script setup lang="ts">
import { downloadFileAPI, getEnterpriseDetailAPI } from '@/apis/enterprise';
import type { EnterpriseDetail, Rent } from '@/types/enterprise';
import { ref } from 'vue';
import { useRoute } from 'vue-router';

const route = useRoute()

const detail = ref<EnterpriseDetail>({} as EnterpriseDetail)

const getEnterpriseDetail = async () => {
  if(route.params.id){
    const res = await getEnterpriseDetailAPI(route.params.id as string)
    console.log('detail',res)
    detail.value = res.data
  }
}

getEnterpriseDetail()
// 文件预览
const doUrl = (url: string) => {
  const extName = url.split('.').pop()
  if(extName === 'pdf'){
    return url
  }else if(extName === 'doc' || extName === 'docx'){
    // 微软提供的文档预览地址
    const MCurl = `https://view.officeapps.live.com/op/view.aspx?src=`
    return MCurl + encodeURIComponent(url)
  }
}
// 文件下载
const downloadFile = async(row: Rent)=>{
  // 根据附件 id 下载文件，返回二进制的 blob 数据
  const res = await downloadFileAPI(row.contractId);
  // 浏览器下载文件用 a 标签
  const aElement = document.createElement("a");
  // 把 blob 数据转换成 url 链接（类似鸿蒙的沙箱路径）
  aElement.href = URL.createObjectURL(res);
  // 设置 download 属性为下载后的合同文件名
  aElement.download = row.contractName;
  // 点击下载
  aElement.click();
  // 释放内存
  URL.revokeObjectURL(aElement.href);
  // 移除a标签
  aElement.remove();
}

</script>

<template>
  <div class="detail-enterprise">
    <header class="add-header">
      <el-page-header content="查看企业" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="e-container">
        <div class="title">企业信息</div>
        <div class="hang">
          <div class="item">企业名称：{{ detail.name }}</div> <div class="item">法人：{{ detail.legalPerson }}</div>
        </div>
        <div class="hang">
          <div class="item">注册地址：{{ detail.registeredAddress }}</div> <div class="item">所在行业：{{ detail.industryName }}</div>
        </div>
        <div class="hangimg">
          <div class="item">
            <div>营业执照</div>
            <el-image class="image" :src="detail.businessLicenseUrl" lazy loading="lazy"></el-image>
          </div>
          <div class="item"></div>
        </div>
      </div>
      <div class="e-container">
        <div class="title">联系人信息</div>
        <div class="hang">
          <div class="item">企业联系人：{{ detail.contact }}</div> <div class="item">联系电话：{{ detail.contactNumber }}</div>
        </div>
      </div>
      <div class="form-container">
        <div class="title">租赁记录</div>
        <div class="table-container">
          <el-table :data="detail.rent" style="width: 100%" border>
            <el-table-column type="index" label="序号" width="50" />
            <el-table-column prop="name" label="租赁楼宇" width="180" />
            <el-table-column label="租赁起止时间" width="280">
              <template #default="{ row }">
                {{ row.startTime }} - {{ row.endTime }}
              </template>
            </el-table-column>
            <el-table-column label="租赁合同(点击预览)">
              <template #default="{ row }">
                <el-button link type="primary">
                  <a class="alink" :href="doUrl(row.contractUrl)" target="_blank">{{ row.contractName }}</a>
                </el-button>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="录入时间" />
            <el-table-column prop="address" label="操作">
              <template #default="{ row }">
                <el-button link type="primary" @click="downloadFile(row)">合同下载</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </main>

  </div>
</template>


<style scoped lang="scss">
.alink{
  text-decoration: none;
  // 继承父级样式
  color: inherit;
}
.detail-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      margin-bottom: 20px;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
    }

    .e-container {
      background-color: #fff;
      margin-bottom: 20px;
      padding: 20px;
      .hang {
        display: flex;
        justify-content: space-around;
        margin-top: 10px;
      }
      .hangimg {
        display: flex;
        margin-top: 10px;

        justify-content: space-around;
        .item {
          display: flex;
          align-self: start;
          .image{
            width: 200px;
          }
        }
      }
    }

    .table-container {
      padding: 20px;
    }

    .preview {
      img {
        width: 100px;
      }
    }
  }
}
</style>